using SanJ.FreeCare.Shared;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Volo.Abp.Application.Dtos;
using SanJ.FreeCare.Medications;

namespace SanJ.FreeCare.Web.Pages.Medications
{
    public abstract class EditModalModelBase : FreeCarePageModel
    {
        [HiddenInput]
        [BindProperty(SupportsGet = true)]
        public Guid Id { get; set; }

        [BindProperty]
        public MedicationUpdateViewModel Medication { get; set; }

        protected IMedicationsAppService _medicationsAppService;

        public EditModalModelBase(IMedicationsAppService medicationsAppService)
        {
            _medicationsAppService = medicationsAppService;

            Medication = new();
        }

        public virtual async Task OnGetAsync()
        {
            var medication = await _medicationsAppService.GetAsync(Id);
            Medication = ObjectMapper.Map<MedicationDto, MedicationUpdateViewModel>(medication);

        }

        public virtual async Task<NoContentResult> OnPostAsync()
        {

            await _medicationsAppService.UpdateAsync(Id, ObjectMapper.Map<MedicationUpdateViewModel, MedicationUpdateDto>(Medication));
            return NoContent();
        }
    }

    public class MedicationUpdateViewModel : MedicationUpdateDto
    {
    }
}